﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using ValidityCheck;
public partial class Admin_MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["admin"] == null)
        {
            Response.Redirect("../Default.aspx");
        }
        InitLinkTable();
        InitMenuAndGuide();
        InitSearchCatalog();
//          if (1 == (int)Session["GroupID"])
//          {
//             MenuUserManagement.Visible = true;
//             MenuUserManagement.Enabled = true;
//             MenuOtherInfoEdit.Visible = true;
//             MenuOtherInfoEdit.Enabled = true;
//             MenuRelativeLinksEdit.Visible = true;
//             MenuRelativeLinksEdit.Enabled = true;
// 
//              UserManage.Visible = true;
//              UserManage.Enabled = true;
//              OtherInfoEditButton.Visible = true;
//              OtherInfoEditButton.Enabled = true;
//              RelativeLinksBotton.Visible = true;
//              RelativeLinksBotton.Enabled = true;
//             MenuPostOnlineEdit.Visible = true;
//             MenuPostOnlineEdit.Enabled = true;
//          } 
//          else
//          {
//             MenuUserManagement.Visible = true;
//             MenuUserManagement.Enabled = false; 
//             MenuOtherInfoEdit.Visible = true;
//             MenuOtherInfoEdit.Enabled = false;
//             MenuRelativeLinksEdit.Visible = true;
//             MenuRelativeLinksEdit.Enabled = false;
//              UserManage.Visible = true;
//              UserManage.Enabled = false;
//              OtherInfoEditButton.Visible = true;
//              OtherInfoEditButton.Enabled = false;
//              RelativeLinksBotton.Visible = true;
//              RelativeLinksBotton.Enabled = false;
//             if (2 == (int)Session["GroupID"])
//             {
//                 MenuPostOnlineEdit.Visible = true;
//                 MenuPostOnlineEdit.Enabled = true;
//             }
//             else
//             {
//                 MenuPostOnlineEdit.Visible = true;
//                 MenuPostOnlineEdit.Enabled = false;
//             }
  /*      }*/
    }


    protected void InitSearchCatalog()
    {
        CatalogDropDownList.Items.Clear();
        ListItem noneListItem = new ListItem("无限制", "0");
        CatalogDropDownList.Items.Add(noneListItem);
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        var Catalogs = from catalog in dc.Catalogs
                       where catalog.Catalog_id != 9
                       select new
                       {
                           Catalog_id = catalog.Catalog_id,
                           CatalogName = catalog.CatalogName
                       };

        foreach (var catalog in Catalogs)
        {
            ListItem theListItem = new ListItem(catalog.CatalogName, catalog.Catalog_id.ToString());
            CatalogDropDownList.Items.Add(theListItem);
        }
    }
     
    protected void InitMenuAndGuide()
    {
        MainMenu.Items.Clear();
        GuideMenu.Items.Clear();
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        var Catalogs = from catalog in dc.Catalogs
                         select new
                         {
                             Catalog_id = catalog.Catalog_id,
                             Name = catalog.CatalogName
                         };
        foreach (var catalog in Catalogs)
        {
            if (catalog.Catalog_id != 9 || (catalog.Catalog_id == 9 && 0 != (int)Session["GroupID"]))
            {
                MenuItem menuitem = new MenuItem();
                menuitem.Enabled = true;
                menuitem.Selectable = true;
                menuitem.Selected = false;
                menuitem.ToolTip = menuitem.Text = catalog.Name + "管理";
                menuitem.Value = catalog.Catalog_id.ToString();
                MainMenu.Items.Add(menuitem);
                menuitem = new MenuItem(); 
                menuitem.Enabled = true;
                menuitem.Selectable = true;
                menuitem.Selected = false;
                menuitem.ToolTip = menuitem.Text = catalog.Name + "管理";
                menuitem.Value = catalog.Catalog_id.ToString();
                GuideMenu.Items.Add(menuitem);
            }
        }
        dc.Dispose();
        MenuItem OtherManagementItem = new MenuItem();
        OtherManagementItem.Enabled = true;
        OtherManagementItem.Selectable = false;
        OtherManagementItem.Selected = false;
        OtherManagementItem.Value = OtherManagementItem.ToolTip = OtherManagementItem.Text = "其他项目管理";
        MainMenu.Items.Add(OtherManagementItem);
//         MenuItem GuideOtherManagementItem = new MenuItem();
//         GuideOtherManagementItem.Enabled = true;
//         GuideOtherManagementItem.Selectable = false;
//         GuideOtherManagementItem.Selected = false;
//         GuideOtherManagementItem.Value = GuideOtherManagementItem.ToolTip = GuideOtherManagementItem.Text = "其他项目管理";
//         GuideMenu.Items.Add(GuideOtherManagementItem);

        MenuItem Password = new MenuItem();
        Password.Enabled = true;
        Password.Selectable = true;
        Password.Selected = false;
        Password.Value = Password.ToolTip = Password.Text = "密码管理";
        OtherManagementItem.ChildItems.Add(Password);
        Password = new MenuItem();
        Password.Enabled = true;
        Password.Selectable = true;
        Password.Selected = false;
        Password.Value = Password.ToolTip = Password.Text = "密码管理";
        GuideMenu.Items.Add(Password);

        if (1== (int)Session["GroupID"])//管理员
        {
            MenuItem User = new MenuItem();
            User.Enabled = true;
            User.Selectable = true;
            User.Selected = false;
            User.Value = User.ToolTip = User.Text = "用户管理";
            OtherManagementItem.ChildItems.Add(User);
            User = new MenuItem();
            User.Enabled = true;
            User.Selectable = true;
            User.Selected = false;
            User.Value = User.ToolTip = User.Text = "用户管理";
            GuideMenu.Items.Add(User);

            MenuItem Links = new MenuItem();
            Links.Enabled = true;
            Links.Selectable = true;
            Links.Selected = false;
            Links.Value = Links.ToolTip = Links.Text = "相关链接管理";
            OtherManagementItem.ChildItems.Add(Links);
            Links = new MenuItem();
            Links.Enabled = true;
            Links.Selectable = true;
            Links.Selected = false;
            Links.Value = Links.ToolTip = Links.Text = "相关链接管理";
            GuideMenu.Items.Add(Links);

            MenuItem MenuCatalogSetting = new MenuItem();
            MenuCatalogSetting.Enabled = true;
            MenuCatalogSetting.Selectable = true;
            MenuCatalogSetting.Selected = false;
            MenuCatalogSetting.Value = MenuCatalogSetting.ToolTip = MenuCatalogSetting.Text = "菜单项管理";
            OtherManagementItem.ChildItems.Add(MenuCatalogSetting);
            MenuCatalogSetting = new MenuItem();
            MenuCatalogSetting.Enabled = true;
            MenuCatalogSetting.Selectable = true;
            MenuCatalogSetting.Selected = false;
            MenuCatalogSetting.Value = MenuCatalogSetting.ToolTip = MenuCatalogSetting.Text = "菜单项管理";
            GuideMenu.Items.Add(MenuCatalogSetting);

            MenuItem LogEdit = new MenuItem();
            LogEdit.Enabled = true;
            LogEdit.Selectable = true;
            LogEdit.Selected = false;
            LogEdit.Value = LogEdit.ToolTip = LogEdit.Text = "网页Log管理";
            OtherManagementItem.ChildItems.Add(LogEdit);
            LogEdit = new MenuItem();
            LogEdit.Enabled = true;
            LogEdit.Selectable = true;
            LogEdit.Selected = false;
            LogEdit.Value = LogEdit.ToolTip = LogEdit.Text = "网页Log管理";
            GuideMenu.Items.Add(LogEdit);

            MenuItem OtherInfor = new MenuItem();
            OtherInfor.Enabled = true;
            OtherInfor.Selectable = true;
            OtherInfor.Selected = false;
            OtherInfor.Value = OtherInfor.ToolTip = OtherInfor.Text = "网页其他信息管理";
            OtherManagementItem.ChildItems.Add(OtherInfor);
            OtherInfor = new MenuItem();
            OtherInfor.Enabled = true;
            OtherInfor.Selectable = true;
            OtherInfor.Selected = false;
            OtherInfor.Value = OtherInfor.ToolTip = OtherInfor.Text = "网页其他信息管理";
            GuideMenu.Items.Add(OtherInfor);

            MenuItem PopUpPages = new MenuItem();
            PopUpPages.Enabled = true;
            PopUpPages.Selectable = true;
            PopUpPages.Selected = false;
            PopUpPages.Value = PopUpPages.ToolTip = PopUpPages.Text = "弹出网页管理";
            OtherManagementItem.ChildItems.Add(PopUpPages);
            PopUpPages = new MenuItem();
            PopUpPages.Enabled = true;
            PopUpPages.Selectable = true;
            PopUpPages.Selected = false;
            PopUpPages.Value = PopUpPages.ToolTip = PopUpPages.Text = "弹出网页管理";
            GuideMenu.Items.Add(PopUpPages); 
        }

        MenuItem Logoff=new MenuItem();
            Logoff.Enabled=true;
            Logoff.Selectable=true;
            Logoff.Selected=false;
            Logoff.Value=Logoff.ToolTip=Logoff.Text="退出登陆";
            MainMenu.Items.Add(Logoff); 
        Logoff = new MenuItem();
            Logoff.Enabled = true;
            Logoff.Selectable = true;
            Logoff.Selected = false;
            Logoff.Value = Logoff.ToolTip = Logoff.Text = "退出登陆";
            GuideMenu.Items.Add(Logoff);
    }

    protected void InitLinkTable()
    {
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        var LinksInfos = from linksinfo in dc.LinksInfos
                         select new
                         {
                             标题 = linksinfo.Title,
                             内容 = linksinfo.Content,
                             发布 = linksinfo.isPublished
                         };
        TableRow tr = new TableRow();
        tr.HorizontalAlign = HorizontalAlign.Center;
        tr.VerticalAlign = VerticalAlign.Middle;
        TableCell tcTitle = new TableCell();
        tcTitle.HorizontalAlign = HorizontalAlign.Center;
        tcTitle.VerticalAlign = VerticalAlign.Middle;
        HyperLink Lbtitle = new HyperLink();
        Lbtitle.Text = "相关链接：";
        tcTitle.Controls.Add(Lbtitle);
        tcTitle.CssClass = "BottomHyperLink";
        tr.Cells.Add(tcTitle);
        foreach (var linksinfo in LinksInfos)
        {
            //          TableCell tcboard = new TableCell();
            //         Label Lbboard = new Label();
            HyperLink link;
            TableCell tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.VerticalAlign = VerticalAlign.Middle;
            link = new HyperLink();
            link.Text = linksinfo.标题;
            link.NavigateUrl = linksinfo.内容;
            link.CssClass = "BottomHyperLink";
            tc.Controls.Add(link);
            tr.Cells.Add(tc);
        }
        Linktable.Rows.Add(tr);
        dc.Dispose();
    }

    protected void Logoff_Click(object sender, EventArgs e)
    {
        Session.Remove("admin");
        Response.Redirect("../Default.aspx");
    }

    protected void ChangePassword_Click(object sender, EventArgs e)
    {
        Response.Redirect("ChangePassword.aspx");
    }
    protected void PictureNewsManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "1");
        Response.Redirect("AdminHome.aspx");
    }
    protected void CollegeNewsManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "2");
        Response.Redirect("AdminHome.aspx");
    }
    protected void NoticeManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "3");
        Response.Redirect("AdminHome.aspx");
    }
    protected void IndependentInstituteDynamicManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "4");
        Response.Redirect("AdminHome.aspx");
    }
    protected void CampusDynamicManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "5");
        Response.Redirect("AdminHome.aspx");
    }
    protected void UserManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "用户管理");
        Response.Redirect("AdminHome.aspx");
    }
    protected void OtherInfoEditButton_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "网页其他信息管理");
        Response.Redirect("AdminHome.aspx");
    }

    protected void PopUpPagesBotton_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "弹出网页管理");
        Response.Redirect("AdminHome.aspx");
    }

    protected void RelativeLinksBotton_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "相关链接管理");
        Response.Redirect("AdminHome.aspx");
    }

    protected void MenuCatalog_Click(object sender, EventArgs e)
    {
        Response.Redirect("MenuCatalogSetting.aspx");
    }
    protected void PostOnlineEdit_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "9");
        Response.Redirect("AdminHome.aspx");
    }

    protected void LogEdit_Click(object sender, EventArgs e)
    {
        Response.Redirect("EditLog.aspx");
    }

    protected void MainMenu_MenuItemClick(object sender, MenuEventArgs e)
    {
        Menu senderMenu=sender as Menu;
        switch (senderMenu.SelectedValue)
        {
            case "1"://picture news management
                PictureNewsManagement_Click(null,null);
                break;
            case "2":
                CollegeNewsManagement_Click(null, null);
                break;
            case "3"://picture news management
                NoticeManagement_Click(null, null);
                break;
            case "4"://picture news management
                IndependentInstituteDynamicManagement_Click(null, null);
                break;
            case "5"://picture news management
                CampusDynamicManagement_Click(null, null);
                break;
            case "9"://picture news management
                PostOnlineEdit_Click(null, null);
                break;
            case "密码管理":
                ChangePassword_Click(null, null);
                break;
            case "用户管理":
                UserManagement_Click(null, null);
                break;
            case "相关链接管理":
                RelativeLinksBotton_Click(null, null);
                break;
            case "网页其他信息管理":
                OtherInfoEditButton_Click(null, null);
                break;
            case "弹出网页管理":
                PopUpPagesBotton_Click(null,null);
                break;
            case "菜单项管理":
                MenuCatalog_Click(null, null);
                break;
            case "网页Log管理":
                LogEdit_Click(null, null);
                break;
            case "退出登陆":
                Logoff_Click(null, null);
                break;
        }
    }
    protected void SearchButton_Click(object sender, EventArgs e)
    {
        if (Check.IsNull(KeyWord.Value))
        {

        }
        else
        {
            Session.Add("InitType", "搜索");
            Session.Add("Search", "true");
            Session.Add("SearchCatalog", CatalogDropDownList.SelectedValue);
            Session.Add("SearchKeyWord", KeyWord.Value);
            Response.Redirect("AdminHome.aspx");
        }
    }
}
